<template>
  <div class="container">
    <div class="top">
      <div class="left">
        <el-input
          v-model="dishParams.keyword"
          placeholder="请输入菜品名称"
          prefix-icon="el-icon-search"
          type="text"
          @input="search"
        />
      </div>
      <div class="right">
        <ul>
          <li><a>批量删除</a></li>
          <li><a>批量启售</a></li>
          <li><a>批量停售</a></li>
          <el-button type="primary" @click="$router.push('/dish/list')"
            >+新建菜品</el-button
          >
        </ul>
      </div>
    </div>
    <div class="main">
      <el-table
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" />
        <el-table-column label="菜品名称" prop="name" />
        <el-table-column label="图片" prop="image">
          <template v-slot="{ row }">
            <img :src="row.image" />
          </template>
        </el-table-column>
        <el-table-column label="菜品分类" prop="categoryName" />
        <el-table-column label="售价" prop="price" />
        <el-table-column label="售卖状态" prop="status">
          <template v-slot="{ row }">
            {{ row.status === 1 ? "启售" : "停售" }}
          </template>
        </el-table-column>
        <el-table-column label="最后操作时间" prop="updateTime" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button type="text" @click="$router.push(`/dish/list`)"
              >修改</el-button
            >
            <el-button type="text" @click="btnStatus(row.id)">{{
              row.status === 1 ? "停售" : "启售"
            }}</el-button>
            <el-button type="text" @click="btnDelete(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row type="flex" justify="center">
        <el-pagination
          :current-page="dishParams.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="dishParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>
  </div>
</template>

<script>
import { changeStatus, deleteDish, dishSearch, getDishList } from "@/api/dish";
export default {
  name: "DishIndex",
  data() {
    return {
      dishParams: {
        page: 1,
        pageSize: 10,
        keyword: "",
      },
      total: 0,
      tableData: [],
      selection: [],
    };
  },
  created() {
    this.getDishList();
  },
  methods: {
    handleSelectionChange(val) {
      this.selection = val;
      console.log(this.selection);
    },
    async getDishList() {
      const res = await getDishList(this.dishParams);
      this.tableData = res.data.records;
      this.total = res.data.total;
    },
    handleCurrentChange(newPage) {
      this.dishParams.page = newPage;
      this.getDishList();
    },
    handleSizeChange(newpageSize) {
      this.dishParams.pageSize = newpageSize;
      this.getDishList();
    },
    btnDelete(id) {
      this.$confirm("你确定要修改状态吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        await deleteDish(id);
        this.getDishList();
        this.$message({
          type: "success",
          message: "删除成功!",
        });
      });
    },
    btnStatus(id) {
      this.$confirm("你确定要修改状态吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        await changeStatus(id);
        this.getDishList();
        this.$message({
          type: "success",
          message: "状态已经修改成功!",
        });
      });
    },
    async search() {
      const res = await dishSearch(this.dishParams);
      this.dishParams = res.data.records;
      this.total = res.data.total;
    },
  },
};
</script>

<style lang="scss" scoped>
.top {
  display: flex;
  justify-content: space-between;
  padding: 30px;
}
.top .left {
  margin-top: 15px;
}
li {
  list-style: none;
}
.top .right ul {
  display: flex;
}
.top .right ul li {
  height: 60px;
  line-height: 60px;
}
.top .right ul a {
  color: rgba(245, 108, 108);
  padding: 0 10px;
  border-right: 1px solid rgba(216, 221, 227);
}
.top .right ul li:nth-child(2) a {
  color: rgba(65, 158, 255);
}
.main {
  padding: 0 30px;
}
</style>
